package property.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByPrimaryKeyElementGenerator;
import property.mapper.*;
import property.pojo.*;
import property.service.QueryService;
import property.utils.mapper.MyBatisUtil;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class QueryServiceImpl implements QueryService {

    @Override
    public  List peopleAndThingQuery(short findType, JSONArray examplesArray) {
        //传入查询类型 + 条件集合
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        //查找用户--1
        if(findType==1)
        {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> users= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                users= JSON.parseArray(examplesArray.toJSONString(),User.class);
            }
            UserExample userExample=new UserExample();//对应一个（）
            byte i=0;
            // 输出转换后的用户信息
            for (User user : users) {
                UserExample.Criteria userExampleCriteria = userExample.createCriteria();//对应一条判断语句

                if(user.getuGender()!=null&&!user.getuGender().isEmpty())
                {
                    userExampleCriteria.andUGenderEqualTo(user.getuGender());
                }
                if(user.getuPhone()!=null&&!user.getuPhone().isEmpty()){
                    userExampleCriteria.andUPhoneLike("%"+user.getuPhone()+"%");
                }
                if(user.getuIdCard()!=null&&!user.getuIdCard().isEmpty()){
                    userExampleCriteria.andUIdCardLike("%"+user.getuIdCard()+"%");
                }
                if(user.getuEmail()!=null&&!user.getuEmail().isEmpty()){
                    userExampleCriteria.andUEmailLike("%"+user.getuEmail()+"%");
                }
                if(user.getuAddress()!=null&&!user.getuAddress().isEmpty()){
                    userExampleCriteria.andUAddressLike("%"+user.getuAddress()+"%");
                }
                if(user.getuLiveTime()!=null&&!Objects.isNull(user.getuLiveTime()))
                {
                    userExampleCriteria.andULiveTimeEqualTo(user.getuLiveTime());
                }
                if(user.getuUserName()!=null&&!user.getuUserName().isEmpty()){
                    userExampleCriteria.andUUserNameLike("%"+user.getuUserName()+"%");
                }
                if(user.getuName()!=null&&!user.getuName().isEmpty()){
                    userExampleCriteria.andUNameLike(user.getuName()+"%");
                }
                i++;
                if(i>1){
                    userExample.or(userExampleCriteria);
                }
            }
            List<User> userList = mapper.selectByExample(userExample);
            return userList;
        }
        //查找员工--2
        if(findType==2)
        {
            StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
            List<Staff> staffs= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                staffs= JSON.parseArray(examplesArray.toJSONString(),Staff.class);
            }
            StaffExample staffExample=new StaffExample();
            byte i=0;
            for (Staff staff:staffs) {

                StaffExample.Criteria staffExampleCriteria = staffExample.createCriteria();
                if(staff.getsGender()!=null&&!staff.getsGender().isEmpty()){
                    staffExampleCriteria.andSGenderEqualTo(staff.getsGender());
                }
                if(staff.getsEmail()!=null&&!staff.getsEmail().isEmpty()){
                    staffExampleCriteria.andSEmailLike("%"+staff.getsEmail()+"%");
                }
                if(staff.getsDepartment()!=null&&!staff.getsDepartment().isEmpty()){
                    staffExampleCriteria.andSDepartmentEqualTo(staff.getsDepartment());
                }
                if(staff.getsAddress()!=null&&!staff.getsAddress().isEmpty()){
                    staffExampleCriteria.andSAddressLike("%"+staff.getsAddress()+"%");
                }
                if(staff.getsPhone()!=null&&!staff.getsPhone().isEmpty()){
                    staffExampleCriteria.andSPhoneLike("%"+staff.getsPhone()+"%");
                }
                if(staff.getsName()!=null&&!staff.getsName().isEmpty()) {
                    staffExampleCriteria.andSNameLike(staff.getsName()+"%");
                }
                if(staff.getsUsername()!=null&&!staff.getsUsername().isEmpty()){
                    staffExampleCriteria.andSUsernameLike("%"+staff.getsUsername()+"%");
                }
                if(staff.getsStatus()!=null){
                    staffExampleCriteria.andSStatusEqualTo(staff.getsStatus());
                }
                if(staff.getsPassword()!=null&&!staff.getsPassword().isEmpty()){
                    staffExampleCriteria.andSPasswordLike("%"+staff.getsPassword()+"%");
                }
                if(staff.getsBirthdate()!=null&&!Objects.isNull(staff.getsBirthdate())){
                    staffExampleCriteria.andSBirthdateEqualTo(staff.getsBirthdate());
                }
                if(staff.getsIdCard()!=null&&!staff.getsIdCard().isEmpty()){
                    staffExampleCriteria.andSIdCardLike("%"+staff.getsIdCard()+"%");
                }
                if(staff.getsPosition()!=null&&!staff.getsPosition().isEmpty()){
                    staffExampleCriteria.andSPositionLike("%"+staff.getsPosition()+"%");
                }
                if(staff.getsEntryDate()!=null&&!Objects.isNull(staff.getsEntryDate())){
                    staffExampleCriteria.andSEntryDateEqualTo(staff.getsEntryDate());
                }
                if(staff.getsSalary()!=null){
                    staffExampleCriteria.andSSalaryEqualTo(staff.getsSalary());
                }
                i++;
                if(i>1){
                    staffExample.or(staffExampleCriteria);
                }
            }
            List<Staff> staffList = mapper.selectByExample(staffExample);
            return staffList;
        }
        //查找管理员--3
        if(findType==3)
        {
            AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
            List<Admin> admins= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                admins= JSON.parseArray(examplesArray.toJSONString(),Admin.class);
            }
            AdminExample adminExample=new AdminExample();
            byte i=0;
            for (Admin admin : admins) {
                AdminExample.Criteria adminExampleCriteria= adminExample.createCriteria();
                if(admin.getaGender()!=null&&!admin.getaGender().isEmpty()){
                    adminExampleCriteria.andAGenderEqualTo(admin.getaGender());
                }
                if(admin.getaPhone()!=null&&!admin.getaPhone().isEmpty()){
                    adminExampleCriteria.andAPhoneLike("%"+admin.getaPhone()+"%");
                }
                if(admin.getaEmail()!=null&&!admin.getaEmail().isEmpty()){
                    adminExampleCriteria.andAEmailLike("%"+admin.getaEmail()+"%");
                }
                if(admin.getaPermission()!=null){
                    adminExampleCriteria.andAPermissionEqualTo(admin.getaPermission());
                }
                if(admin.getaName()!=null&&!admin.getaName().isEmpty()){
                    adminExampleCriteria.andANameLike(admin.getaName()+"%");
                }
                if(admin.getaUserName()!=null&&!admin.getaUserName().isEmpty()){
                    adminExampleCriteria.andAUserNameLike("%"+admin.getaUserName()+"%");
                }
                if(admin.getaEntryDate()!=null&&!Objects.isNull(admin.getaEntryDate())){
                    adminExampleCriteria.andAEntryDateEqualTo(admin.getaEntryDate());
                }
                if(admin.getaBirthdate()!=null&&!Objects.isNull(admin.getaBirthdate())){
                    adminExampleCriteria.andABirthdateEqualTo(admin.getaBirthdate());
                }
                if(admin.getaIdCard()!=null&&!admin.getaIdCard().isEmpty()){
                    adminExampleCriteria.andAIdCardLike("%"+admin.getaIdCard()+"%");
                }
                if(admin.getaPassword()!=null&&admin.getaPassword().isEmpty()){
                    adminExampleCriteria.andAIdCardLike("%"+admin.getaPassword()+"%");
                }
                i++;
                if(i>1){
                    adminExample.or(adminExampleCriteria);
                }
            }
            List<Admin> adminList = mapper.selectByExample(adminExample);
            return adminList;
        }
        //查找反馈--4
        if(findType==4)
        {
            FeedbackMapper mapper = sqlSession.getMapper(FeedbackMapper.class);
            //将条件列表转为对应类型的list集合
            List<Feedback> feedbacks= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                feedbacks= JSON.parseArray(examplesArray.toJSONString(),Feedback.class);
            }
            FeedbackExample feedbackExample=new FeedbackExample();//对应一个（）
            byte i=0;

            for (Feedback feedback : feedbacks) {
                FeedbackExample.Criteria feedbackExampleCriteria = feedbackExample.createCriteria();//对应一条判断语句

                if(feedback.getFbUserId()!=null){
                    feedbackExampleCriteria.andFbUserIdEqualTo(feedback.getFbUserId());
                }
                if(feedback.getFbSubmitDate()!=null&&!Objects.isNull(feedback.getFbSubmitDate())){
                    feedbackExampleCriteria.andFbSubmitDateEqualTo(feedback.getFbSubmitDate());
                }
                if(feedback.getFbStatus()!=null){
                    feedbackExampleCriteria.andFbStatusEqualTo(feedback.getFbStatus());
                }
                if(feedback.getFbType()!=null){
                    feedbackExampleCriteria.andFbTypeEqualTo(feedback.getFbType());
                }
                if(feedback.getFbSatisfaction()!=null){
                    feedbackExampleCriteria.andFbSatisfactionEqualTo(feedback.getFbSatisfaction());
                }

                i++;
                if(i>1){
                    feedbackExample.or(feedbackExampleCriteria);
                }
            }

            List<Feedback>  feedbackList= mapper.selectByExample(feedbackExample);
            return feedbackList;

        }
        //查找反馈处理--5
        if(findType==5)
        {
            FeedbackDisposeMapper mapper = sqlSession.getMapper(FeedbackDisposeMapper.class);
            List<FeedbackDispose> feedbackDisposes= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                feedbackDisposes= JSON.parseArray(examplesArray.toJSONString(),FeedbackDispose.class);
            }
            FeedbackDisposeExample feedbackDisposeExample=new FeedbackDisposeExample();//对应一个（）
            byte i=0;

            for (FeedbackDispose feedbackDispose : feedbackDisposes) {
                FeedbackDisposeExample.Criteria feedbackDisposeExampleCriteria = feedbackDisposeExample.createCriteria();//对应一条判断语句

                if(feedbackDispose.getFdStatus()!=null){
                    feedbackDisposeExampleCriteria.andFdStatusEqualTo(feedbackDispose.getFdStatus());
                }
                if(feedbackDispose.getFdDisposeTime()!=null&&!Objects.isNull(feedbackDispose.getFdDisposeTime())){
                    feedbackDisposeExampleCriteria.andFdDisposeTimeEqualTo(feedbackDispose.getFdDisposeTime());
                }
                if(feedbackDispose.getFdStaffId()!=null){
                    feedbackDisposeExampleCriteria.andFdStaffIdEqualTo(feedbackDispose.getFdStaffId());
                }
                if(feedbackDispose.getFdFeedbackId()!=null)
                    feedbackDisposeExampleCriteria.andFdFeedbackIdEqualTo(feedbackDispose.getFdFeedbackId());
                i++;
                if(i>1){
                    feedbackDisposeExample.or(feedbackDisposeExampleCriteria);
                }
            }
            List<FeedbackDispose> feedbackDisposeList = mapper.selectByExample(feedbackDisposeExample);
            return feedbackDisposeList;
        }
        //查找财务信息--6
        if(findType==6)
        {
            FinanceMapper mapper = sqlSession.getMapper(FinanceMapper.class);
            List<Finance> finances= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                finances= JSON.parseArray(examplesArray.toJSONString(),Finance.class);
            }
            FinanceExample financeExample=new FinanceExample();//对应一个（）
            byte i=0;

            for (Finance finance : finances) {
                FinanceExample.Criteria financeExampleCriteria = financeExample.createCriteria();//对应一条判断语句
               if(finance.getfDate()!=null&&!Objects.isNull(finance.getfDate())){
                   financeExampleCriteria.andFDateEqualTo(finance.getfDate());
               }
               if(finance.getfStatus()!=null){
                   financeExampleCriteria.andFStatusEqualTo(finance.getfStatus());
               }
               if(finance.getfStaffId()!=null){
                   financeExampleCriteria.andFStaffIdEqualTo(finance.getfStaffId());
               }
               if(finance.getfAdminId()!=null){
                   financeExampleCriteria.andFAdminIdEqualTo(finance.getfAdminId());
               }
               if(finance.getfType()!=null&&!finance.getfType().isEmpty()){
                   financeExampleCriteria.andFTypeLike("%"+finance.getfType()+"%");
               }
               if(finance.getfStakeholder()!=null&&!finance.getfStakeholder().isEmpty()){
                   financeExampleCriteria.andFStakeholderLike("%"+finance.getfStakeholder()+"%");
               }
               if(finance.getfAmount()!=null){
                   financeExampleCriteria.andFAmountEqualTo(finance.getfAmount());
               }
               if(finance.getfNumber()!=null){
                   financeExampleCriteria.andFNumberEqualTo(finance.getfNumber());
               }
                i++;
                if(i>1){
                    financeExample.or(financeExampleCriteria);
                }
            }
            List<Finance> financeList = mapper.selectByExample(financeExample);
            return financeList;
        }
        //查找设施--7
        if(findType==7)
        {
            InfrastructureMapper mapper = sqlSession.getMapper(InfrastructureMapper.class);
            List<Infrastructure> infrastructures= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                infrastructures= JSON.parseArray(examplesArray.toJSONString(),Infrastructure.class);
            }
            InfrastructureExample infrastructureExample=new InfrastructureExample();
            byte i=0;

            for (Infrastructure infrastructure : infrastructures) {
                InfrastructureExample.Criteria infrastructureExampleCriteria = infrastructureExample.createCriteria();
                if(infrastructure.getiOverhaulTime()!=null&&!Objects.isNull(infrastructure.getiOverhaulTime())){
                    infrastructureExampleCriteria.andIOverhaulTimeEqualTo(infrastructure.getiOverhaulTime());
                }
                if(infrastructure.getiStaffId()!=null){
                    infrastructureExampleCriteria.andIStaffIdEqualTo(infrastructure.getiStaffId());
                }
                if(infrastructure.getiStatus()!=null){
                    infrastructureExampleCriteria.andIStatusEqualTo(infrastructure.getiStatus());
                }
                if(infrastructure.getiName()!=null&&!infrastructure.getiName().isEmpty()){
                    infrastructureExampleCriteria.andINameLike("%"+infrastructure.getiName()+"%");
                }
                if(infrastructure.getiType()!=null&&!infrastructure.getiType().isEmpty()){
                    infrastructureExampleCriteria.andITypeLike("%"+infrastructure.getiType()+"%");
                }
                if(infrastructure.getiLocation()!=null&&!infrastructure.getiLocation().isEmpty()){
                    infrastructureExampleCriteria.andILocationLike("%"+infrastructure.getiLocation()+"%");
                }
                if(infrastructure.getiInstallDate()!=null&&!Objects.isNull(infrastructure.getiInstallDate())){
                    infrastructureExampleCriteria.andIInstallDateEqualTo(infrastructure.getiInstallDate());
                }
                if(infrastructure.getiPrice()!=null){
                    infrastructureExampleCriteria.andIPriceEqualTo(infrastructure.getiPrice());
                }
                if(infrastructure.getiFinanceId()!=null){
                    infrastructureExampleCriteria.andIFinanceIdEqualTo(infrastructure.getiFinanceId());
                }
                i++;
                if(i>1){
                    infrastructureExample.or(infrastructureExampleCriteria);
                }
            }
            List<Infrastructure> infrastructureList = mapper.selectByExample(infrastructureExample);
            return infrastructureList;

        }
        //查找公告--8
        if(findType==8)
        {
            NoticeMapper mapper = sqlSession.getMapper(NoticeMapper.class);
            List<Notice> notices= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                notices= JSON.parseArray(examplesArray.toJSONString(),Notice.class);
            }
            NoticeExample noticeExample=new NoticeExample();
            byte i=0;
            for (Notice notice : notices) {
                NoticeExample.Criteria noticeExampleCriteria = noticeExample.createCriteria();
                if(notice.getnPublishDate()!=null&&!Objects.isNull(notice.getnPublishDate())){
                    noticeExampleCriteria.andNPublishDateEqualTo(notice.getnPublishDate());
                }
                if(notice.getnTag()!=null&&!notice.getnTag().isEmpty()){
                    noticeExampleCriteria.andNTagLike("%"+notice.getnTag()+"%");
                }
                if(notice.getnAdminId()!=null){
                    noticeExampleCriteria.andNAdminIdEqualTo(notice.getnAdminId());
                }
                if(notice.getnTarget()!=null){
                    noticeExampleCriteria.andNTargetEqualTo(notice.getnTarget());
                }
                if(notice.getnTitle()!=null&&!notice.getnTitle().isEmpty()){
                    noticeExampleCriteria.andNTitleLike("%"+notice.getnTitle()+"%");
                }
                if(notice.getnContent()!=null&&!notice.getnContent().isEmpty()){
                    noticeExampleCriteria.andNContentLike("%"+notice.getnContent()+"%");
                }
                if(notice.getnGrade()!=null){
                    noticeExampleCriteria.andNGradeEqualTo(notice.getnGrade());
                }
                if(notice.getnStatus()!=null){
                    noticeExampleCriteria.andNStatusEqualTo(notice.getnStatus());
                }
                if(notice.getnCategory()!=null&&!notice.getnCategory().isEmpty()){
                    noticeExampleCriteria.andNCategoryLike("%"+notice.getnCategory()+"%");
                }
                i++;
                if(i>1){
                    noticeExample.or(noticeExampleCriteria);
                }
            }
            List<Notice> noticeList = mapper.selectByExample(noticeExample);
            return noticeList;
        }
        //查找检修--9
        if(findType==9)
        {
            OverhaulMapper mapper = sqlSession.getMapper(OverhaulMapper.class);
            List<Overhaul> overhauls= new ArrayList<>();
            //将条件列表转为对应类型的list集合
            if(examplesArray!=null){
                overhauls= JSON.parseArray(examplesArray.toJSONString(),Overhaul.class);
            }
            OverhaulExample overhaulExample=new OverhaulExample();
            byte i=0;

            for (Overhaul overhaul : overhauls) {
                OverhaulExample.Criteria overhaulExampleCriteria = overhaulExample.createCriteria();
                if(overhaul.getoStartDate()!=null&!Objects.isNull(overhaul.getoStartDate())){
                    overhaulExampleCriteria.andOStartDateEqualTo(overhaul.getoStartDate());
                }
                if(overhaul.getoEndDate()!=null&&!Objects.isNull(overhaul.getoEndDate())){
                    overhaulExampleCriteria.andOEndDateEqualTo(overhaul.getoEndDate());
                }
                if(overhaul.getoStaffId()!=null){
                    overhaulExampleCriteria.andOStaffIdEqualTo(overhaul.getoStaffId());
                }
                if(overhaul.getoStatus()!=null){
                    overhaulExampleCriteria.andOStatusEqualTo(overhaul.getoStatus());
                }
                if(overhaul.getoInfraId()!=null){
                    overhaulExampleCriteria.andOInfraIdEqualTo(overhaul.getoInfraId());
                }
                i++;
                if(i>1){
                    overhaulExample.or(overhaulExampleCriteria);
                }
            }
            List<Overhaul> overhaulList = mapper.selectByExample(overhaulExample);
            return overhaulList;
        }
        return null;
    }
    @Override
    public <T>List  peopleLogin(short loginType, T example) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        if(loginType==0)
        {
            //用户
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            UserExample userExample=(UserExample) example;
            List<User> users = mapper.selectByExample(userExample);
            return users;

        }else if(loginType==1){
            //工作人员
            StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
            StaffExample staffExample=(StaffExample) example;
            List<Staff> staffs = mapper.selectByExample(staffExample);
            return staffs;
        }
        else if(loginType==2)
        {
            //管理员
            AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
            AdminExample adminExample=(AdminExample)example;
            List<Admin> admins = mapper.selectByExample(adminExample);
            return admins;
        }
        return null;
    }

}
